﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
using System.Threading;
using Widget.Lib;

namespace Widget.Service
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class StaticAdminService : IChatService
    {
        string msgKey = "chat";
        string user = "api";
        string email = "api@api.com";

        string userTest = "test1";
        string emailTest = "test1@domain.com";
        static Dictionary<string, string> UserCollection = new Dictionary<string, string>();

        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        public ChatMessageCollection GetMessages(string token, string user, string contact)
        {
            if (ValidateUser(token) == false) return null;
            ChatMessageCollection messages = Stores.Get(msgKey);
            var usr = (from m in messages where m.FromContact == user && m.ToContact == contact select m);
            ChatMessageCollection userMessages = new ChatMessageCollection(usr.ToList());
            return userMessages;
        }

        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        public string SetMessage(string token, string user, string contact, string message)
        {
            if (ValidateUser(token) == false) return null;
            string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            Stores.Add(msgKey, user, contact, message, ip);
            return token;
        }

        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        public string Authenticate(string token, string user, string secret)
        {
            return null;
        }

        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        public string GetToken(string token, string user, string email)
        {
            string tk = token;
            string[] apiuser = WebEncrypter.Login.GetParts(token);
            if (apiuser.Length < 3) return "";
            string u = apiuser[0];
            string e = apiuser[1];
            string s = apiuser[2];

            string rand = HttpContext.Current.Session.LCID.ToString();

            if (IsSubscribed(token))
            {
                tk = WebEncrypter.Login.GetToken(user, email, rand);
                UserCollection[user] = tk;
            }
            return tk;
        }
        private bool IsSubscribed(string token)
        {
            string[] apiuser = WebEncrypter.Login.GetParts(token);
            if (apiuser.Length < 3) return false;
            if (userTest  == apiuser[0] && emailTest == apiuser[1])
                return true;
            else
                return false;
        }

        private bool ValidateUser(string token)
        {
            string[] apiuser = WebEncrypter.Login.GetParts(token);
            if (apiuser.Length < 3) return  false;
            string s = apiuser[2];
            string rand = HttpContext.Current.Session.LCID.ToString();
            if (s == rand ) return true;
            return false;
        }
    }

    public class Stores
    {
        static ChatMessageCollection _store_ = new ChatMessageCollection();
        public static ChatMessage Add(string key, string user, string contact, string msg, string ip)
        {
            ChatMessageCollection all = Get(key);
            ChatMessage m = new ChatMessage() { FromContact = user, ID = new Random().Next(100000), Message = msg, ToContact = contact, IP = ip };
            all.Add(m);
            Set(key, all);
            return m;
        }
        public static ChatMessageCollection Get(string key)
        {
            return _store_;
            //ChatMessageCollection messages = HttpContext.Current.Cache[key] as ChatMessageCollection;
            //if (messages == null)
            //{
            //    messages = new ChatMessageCollection();
            //    for (int i = 0; i < 5; i++)
            //    {
            //        messages.Add(new ChatMessage()
            //        {
            //            FromContact = "pijush",
            //            ID = Guid.NewGuid().ToString(),
            //            IP = "127.0.0.1",
            //            Message = "Good Message at " + DateTime.Now.ToString("dd-MM-yyyy"),
            //            ToContact = "unknown"
            //        }
            //            );
            //        Thread.SpinWait(100000);
            //    }
            //}

        }

        public static void Set(string key, ChatMessageCollection message)
        {
            //HttpContext.Current.Cache[key] = message;
            _store_ = message;
        }
    }

    
      
}
